package cn.yqn.user.controller;

import cn.yqn.basic.exception.BusinessException;
import cn.yqn.basic.utils.JsonResult;
import cn.yqn.user.dto.LoginDto;
import cn.yqn.user.dto.WxDto;
import cn.yqn.user.service.ILoginService;
import io.lettuce.core.dynamic.batch.BatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    // 普通登录
    @PostMapping("/account")
    public JsonResult account(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = loginService.account(loginDto);
            return JsonResult.my().setResultObj(map);
        } catch (BatchException e) {
            e.printStackTrace();
            return JsonResult.my().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setMessage("密码或账号错误!");
        }
    }

    // 微信登录
    @GetMapping("/weChat/{code}")
    public JsonResult weChat(@PathVariable("code") String code){
        try {
            // service直接返回一个jsonResoult
            return loginService.weChat(code);

        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.my().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("网络繁忙！");
        }
    }

    // 微信绑定
    @PostMapping("/wechat/binder")
    public JsonResult binder(@RequestBody WxDto dto){
        try {
            // service直接返回一个jsonResoult
            return loginService.binder(dto);
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.my().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("网络繁忙！");
        }
    }




}
